//
//  KWTAddPlantGroupCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/15.
//

#import "KWTAddPlantGroupCell.h"
@interface KWTAddPlantGroupCell()<QMUITextFieldDelegate>
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic ,strong) UIView *line;

@property (nonatomic, strong) MASConstraint *rightConstraint;


@end
@implementation KWTAddPlantGroupCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setCornerRadius
{
    self.bgView.layer.cornerRadius = 8;
    self.bgView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    self.bgView.layer.masksToBounds = YES;
}

-(void)hideLine:(BOOL)hide
{
    self.line.hidden = hide;
}

- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    _titleLab.numberOfLines = 0;
    [_titleLab setFont:[UIFont systemFontOfSize:14]];
    [_titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_titleLab];
    
    
    UIImageView *arrowImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_down"]];
    [bgView addSubview:arrowImg];
    self.arrowImg = arrowImg;
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
    
   
    
    _textField = [[QMUITextField alloc] init];
    [_textField setTextAlignment:NSTextAlignmentRight];
    [_textField setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [_textField setFont:[UIFont systemFontOfSize:14]];
    [_textField setPlaceholder:@"Please input"];
    _textField.delegate = self;
    [bgView addSubview:_textField];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];
    self.line = lineV;
    
    kWeakSelf;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
        make.height.mas_equalTo(50);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.width.mas_equalTo(kScaleW(100));
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
       self.rightConstraint = make.right.equalTo(arrowImg.mas_left).offset(-5);
        make.height.mas_equalTo(30);
        make.centerY.mas_offset(0);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    
    [_titleLab setText:@"title"];
    
    
    QMUIButton *btn = [[QMUIButton alloc]init];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 15, 10, 15));
    }];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.clickBlock){
            weakSelf.clickBlock();
        }
    }];
}

-(BOOL)textField:(QMUITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //删除
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if ([self.titleLab.text isEqualToString:@"电价配置".rh_localized]) {
        if ([textField.text containsString:@"."]) {
            //有小数点就不让再次输入小数点
            if ([string containsString:@"."]||textField.text.length == textField.maximumTextLength) {
                return NO;
            }
            //只能输入两位小数
            //小数点的位置
            NSRange point_range = [textField.text rangeOfString:@"."];
            //在小数点前面可以输入
            if (range.location <= point_range.location) {
                return YES;
            }
            NSString *pointValue = [textField.text substringFromIndex:point_range.location+1];
            NSLog(@"------>%@",pointValue);
            if (pointValue.length>=2) {
                return NO;
            }
            return YES;
        }else{
            //没有小数点存在,如果第一个输入的是小数点,自动补0
            if ([string isEqualToString:@"."]&&textField.text.length == 0) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    textField.text = [NSString stringWithFormat:@"0%@",string];
                });
                return YES;
            }
        }
    }else{
        //其他的按照最大长度限制
        if (textField.text.length>textField.maximumTextLength) {
            return NO;
        }
    }
    return YES;
}


@end
